﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Enterprise.Dal
{
    /// <summary>
    /// 排序枚举
    /// </summary>
    public enum OrderType
    {
        /// <summary>
        /// 降序
        /// </summary>
        DESC,
        /// <summary>
        /// 升序
        /// </summary>
        ASC,
    }

    /// <summary>
    /// AspNet分页类
    /// </summary>
    public class AspNetPager
    {
        /// <summary>
        /// 分页代码
        /// </summary>
        /// <param name="primaryKey">主键字段</param>
        /// <param name="orderfldName">要排序的字段，可以为null,自动取主键排序</param>
        /// <param name="strGetFields">要获取的字段</param>
        /// <param name="tableName">要分页的表名</param>
        /// <param name="where">分页的条件</param>
        /// <param name="pageIndex">当前的页码</param>
        /// <param name="PageSize">一页显示的记录数</param>
        /// <param name="orderType">排序的顺序</param>
        /// <returns></returns>
        public static SqlDataReader DataPager(string primaryKey, string orderfldName, string strGetFields, string tableName, string where, AspNetPagerModel pagerModel, OrderType orderType)
        {
            string strOrder = "";
            string pagerSqlCmd = "";
            if (orderType == OrderType.DESC)
            {
                strOrder = " ORDER BY " + orderfldName + " DESC";
            }
            else
            {
                strOrder = " ORDER BY " + orderfldName + " ASC";
            }
            if (pagerModel.CurrentPageIndex == 1)
            {
                pagerSqlCmd = "SELECT TOP " + pagerModel.PageSize + " " + strGetFields + " FROM " + tableName + " WHERE " + where + "" + strOrder;

            }
            else
            {
                pagerSqlCmd = "SELECT TOP " + pagerModel.PageSize + " " + strGetFields + " FROM " + tableName + " WHERE 1=1 and " +
                    primaryKey + " not in(select top " + pagerModel.PageSize * (pagerModel.CurrentPageIndex - 1) + " " + primaryKey +
                    " from " + tableName + " where " + where + " " + strOrder + ")";
                if (!string.IsNullOrEmpty(where))
                {
                    pagerSqlCmd += " and (" + where + ")";
                }
                pagerSqlCmd += strOrder;
            }
            pagerModel.Count = AspNetPager.GetCount(where, tableName);
            return SqlHelper.ExecuteReader(SqlHelper.connectionString, CommandType.Text, pagerSqlCmd, null);
        }

        /// <summary>
        /// 查询总共有多少条数据
        /// </summary>
        /// <param name="where">条件</param>
        /// <param name="tableName">表名</param>
        /// <returns></returns>
        public static int GetCount(string where, string tableName)
        {
            string sql = "select Count(*) from " + tableName + " where 1 = 1 ";
            if (!string.IsNullOrEmpty(where))
            {
                sql += " and " + where;
            }
            return Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.connectionString, CommandType.Text, sql, null));
        }
    }
}
